/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.alien.admin.modules.publisher.rest;

import com.alien.admin.annotation.Log;
import com.alien.admin.modules.deploy.domain.IdAndNameVo;
import com.alien.admin.modules.publisher.domain.PublishSheetFlow;
import com.alien.admin.modules.publisher.service.PublishSheetFlowService;
import com.alien.admin.modules.publisher.service.dto.PublishSheetFlowDto;
import com.alien.admin.modules.publisher.service.dto.PublishSheetFlowQueryCriteria;
import com.alien.admin.modules.publisher.service.impl.PublishSheetFlowUtils;
import com.alien.admin.modules.system.service.UserService;
import com.alien.admin.utils.enums.FlowNodeIdEnum;
import com.alien.admin.utils.enums.FlowStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zengzhifang
 * @website https://el-admin.vip
 * @date 2021-10-18
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "发布申请单管理")
@RequestMapping("/api/publishSheetFlow")
@Slf4j
public class PublishSheetFlowController {

    private final PublishSheetFlowService publishSheetFlowService;
    private final UserService userService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('publishSheetFlow:list')")
    public void download(HttpServletResponse response, PublishSheetFlowQueryCriteria criteria) throws IOException {
        publishSheetFlowService.download(publishSheetFlowService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询发布申请单")
    @ApiOperation("查询发布申请单")
    @PreAuthorize("@el.check('publishSheetFlow:list')")
    public ResponseEntity<Object> query(PublishSheetFlowQueryCriteria criteria, Pageable pageable) {
        Map<String, Object> map = publishSheetFlowService.queryAll(criteria, pageable);
        List<PublishSheetFlowDto> list = (List<PublishSheetFlowDto>) map.get("content");
        for (PublishSheetFlowDto dto : list) {
            dto.setApplierName(userService.getUserNameById(dto.getApplierId()));
            dto.setDeployerName(userService.getUserNameById(dto.getDeployerId()));
            dto.setManagerName(userService.getUserNameById(dto.getManagerId()));
            dto.setTesterName(userService.getUserNameById(dto.getTesterId()));
            dto.setProcesserName(userService.getUserNameById(dto.getProcesserId()));
        }
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    @GetMapping(value = "getFlowStatus")
    @Log("获取流程状态枚举")
    @ApiOperation("获取流程状态枚举")
    @PreAuthorize("@el.check('publishSheetFlow:list')")
    public ResponseEntity<Object> getFlowStatus() {
        FlowStatusEnum[] arr = FlowStatusEnum.values();
        List<IdAndNameVo> voList = new ArrayList<>();
        for (FlowStatusEnum e : arr) {
            IdAndNameVo vo = new IdAndNameVo();
            vo.setId(e.getId());
            vo.setName(vo.getName());
        }
        return new ResponseEntity<>(voList, HttpStatus.OK);
    }

    @GetMapping(value = "getFlowNodeById")
    @Log("获取流程节点")
    @ApiOperation("获取流程节点")
    @PreAuthorize("@el.check('publishSheetFlow:list')")
    public ResponseEntity<Object> getFlowNodeById(Integer id) {
        FlowNodeIdEnum nodeIdEnum = FlowNodeIdEnum.getById(id);
        if (nodeIdEnum == null) {
            return new ResponseEntity<>(new IdAndNameVo(), HttpStatus.OK);
        } else {
            IdAndNameVo vo = new IdAndNameVo();
            vo.setId(nodeIdEnum.getId());
            vo.setName(nodeIdEnum.getName());
            return new ResponseEntity<>(vo, HttpStatus.OK);
        }
    }

    @GetMapping(value = "findById")
    @Log("查询发布申请单")
    @ApiOperation("查询发布申请单")
    @PreAuthorize("@el.check('publishSheetFlow:list')")
    public ResponseEntity<Object> findById(Long id) {
        return new ResponseEntity<>(publishSheetFlowService.findById(id), HttpStatus.OK);
    }

    @GetMapping(value = "getPublishSheetNo")
    @Log("获取申请单号")
    @ApiOperation("获取申请单号")
    @PreAuthorize("@el.check('publishSheetFlow:list')")
    public ResponseEntity<Object> getPublishSheetNo() {
        return new ResponseEntity<>(PublishSheetFlowUtils.getPublishSheetNo(), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增发布申请单")
    @ApiOperation("新增发布申请单")
    @PreAuthorize("@el.check('publishSheetFlow:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody PublishSheetFlow resources) {
        return new ResponseEntity<>(publishSheetFlowService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改发布申请单")
    @ApiOperation("修改发布申请单")
    @PreAuthorize("@el.check('publishSheetFlow:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody PublishSheetFlow resources) {
        publishSheetFlowService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除发布申请单")
    @ApiOperation("删除发布申请单")
    @PreAuthorize("@el.check('publishSheetFlow:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        publishSheetFlowService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/deploy")
    @Log("部署申请单内服务")
    @ApiOperation("部署申请单内服务")
    @PreAuthorize("@el.check('publishSheetFlow:list')")
    public ResponseEntity<Object> deploy(@RequestParam Long flowId) {
        publishSheetFlowService.deploy(flowId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping(value = "/queryTodayList")
    @Log("查询今日待部署")
    @ApiOperation("查询今日待部署")
    @PreAuthorize("@el.check('publishDeploy:list')")
    public ResponseEntity<Object> queryTodayList(){
        return new ResponseEntity<>(publishSheetFlowService.queryTodayList(),HttpStatus.OK);
    }
}
